package com.tms.app.tms.ui.home.msg;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tms.app.tms.R;
import com.tms.app.tms.adapter.MsgChangeAdapter;
import com.tms.app.tms.adapter.OrderChildMsgAdapter;
import com.tms.app.tms.bean.Msg;
import com.tms.app.tms.bean.NotionBean;
import com.tms.app.tms.db.MsgDBUtil;
import com.tms.app.tms.ui.base.ui.TabFragment;
import com.tms.app.tms.utils.SharePreferenceUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by happy on 2017/8/26.
 */

public class MsgChangeFt extends TabFragment implements SwipeRefreshLayout.OnRefreshListener {
    Unbinder unbinder;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    MsgChangeP msgChangeP;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.layout_fragment_order_child, null);
        unbinder = ButterKnife.bind(this, rootView);
        msgChangeP=new MsgChangeP();
        msgChangeP.setiView(this);
        initView();
        initData();

        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void  showNotion(NotionBean notionBean){
        if (notionBean!=null&&notionBean.getIsFilter()==1)
        {
            return;
        }
        if (!isHidden()){
            showLoadingDialog();
        }
        initData();
    }

    private void initView() {
        LinearLayoutManager mgr = new LinearLayoutManager(getActivity());
        adapter = new MsgChangeAdapter();
        recyclerView.setLayoutManager(mgr);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
        swipeLayout.setOnRefreshListener(this);
//        adapter.setOnItemChildClickListener(msgChangeP);
//        adapter.setOnItemClickListener(msgChangeP);

    }
    private void initData(){
        List<Msg> msgList= MsgDBUtil.getMsgListByType(2);
        if (msgList!=null&&msgList.size()>0){
            adapter.getData().clear();
            adapter.getData().addAll(msgList);
            adapter.notifyDataSetChanged();
            hideNerError();
        }
        hideLoadingDialog();
    }




    @Override
    protected void lazyLoad() {
//        MainActivity activity= (MainActivity) getActivity();
//        activity.hideMsgNum();
            SharePreferenceUtil.setValue(getContext(),"tzhi-BG",0);//
            NotionBean notionBean=new NotionBean();
            notionBean.setIsFilter(1);
            EventBus.getDefault().post(new NotionBean());
    }

    @Override
    public void onRefresh() {
        initData();
        swipeLayout.setRefreshing(false);

    }
}
